<!DOCTYPE html>
<html lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<title></title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
	<meta name="description" content="">
	<meta name="author" content="">
	<!-- STYLESHEETS -->
	<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
	<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
	<link rel="stylesheet" type="text/css" href="../../css/responsive.css">
	<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="../../js/ztree/bootstrapStyle.css">
	<link rel="stylesheet" type="text/css"
	href="../../js/ztree/bootstrapStyle.css">
	<style>
		.ztree li a.curSelectedNode_Edit {
			display: none;
		} 
		#YijiadianCopy,
		#YiigooCopy {
			display: none;
		}
		.data-list .col-md-12{
			margin: 5px 0;
		}
	</style>
</head>
<body>
	<header class="navbar clearfix" id="header"> </header>
	<!-- PAGE -->
	<section id="page">
		<!-- SIDEBAR -->
		<div id="sidebar" class="sidebar">
			<div class="sidebar-menu nav-collapse">
				<!-- SIDEBAR MENU -->
				<ul>
				</ul>
				<!-- /SIDEBAR MENU -->
			</div>
		</div>
		<!-- /SIDEBAR -->
		<div id="main-content">
			<!-- /SAMPLE BOX CONFIGURATION MODAL FORM-->
			<div class="container">
				<div class="row">
					<div id="content" class="col-lg-12">
						<!-- PAGE HEADER-->
						<div class="row" style="height: 30px;">
							<div class="col-sm-12">
								<div class="page-header">
									<!-- STYLER -->
									<!-- /STYLER -->
									<!-- BREADCRUMBS -->
									<ul class="breadcrumb">
										<li><i class="fa fa-home"></i> <a href="../index.html">首页</a>
										</li>
										<li><a href="#">合同管理</a></li>
										<li>商品分类</li>
									</ul>
									<!-- /BREADCRUMBS -->
								</div>
							</div>
						</div>
						<!-- /PAGE HEADER -->
						<div class="row col-md-12">
							<span style="color:red">壹果商城左侧为前台页面显示文字</span><span>右侧分类需要显示的分类数据</span>
						</div>
						<div class="row col-md-12 data-list" name="Yiigoo">
							<div class="col-md-12"><button type="button" class="btn btn-primary btn-add" sales="Yiigoo">添加分类数据</button></div>
						</div>
						<div class="row">
							<!-- TABLES -->
							<div class="col-md-12" style="margin-top: 20px;">
								<button style="margin-left: 30%;" type="button" class="btn btn-success bt_updateTab" sales="Yiigoo">保存</button>
							</div>
						</div>
<!-- 
						<div class="row col-md-12" style="margin-top:20px">
							<span style="color:red">壹家店左侧为前台页面显示文字</span><span>右侧分类需要显示的分类数据</span>
						</div> -->
						<!-- <div class="row col-md-12 data-list" name="Yijiadian">
							<div class="col-md-12"><button type="button" class="btn btn-primary btn-add" sales="Yijiadian">添加分类数据</button></div>
						</div> -->
						<!-- TABLES -->
						<!-- <div class="row">
							<div class="col-md-12" style="margin-left: 30%;margin-top: 20px;">
								<button type="button" class="btn btn-success bt_updateTab" sales="Yijiadian">保存</button>
							</div>
						</div> -->
						<!-- /TABLES -->
						<!-- /CONTENT-->
						<div>
							<div class="col-md-12">
								<div class="form-group mrg-bt10">
									<a class="btn btn-primary config create">
										<i class="fa fa-plus-square-o"></i> 新增一级分类
									</a>
									<a class="btn btn-success config save-yijiadian"> 保存
									</a>
								</div>
								<!-- BOX -->
								<div class="box border primary">
									<ul id="treeDemo" class="ztree"></ul>
								</div>
								<!-- /BOX -->
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="row col-md-12" name="tablist" id='YijiadianCopy'>
				<div class="col-md-2" style="display:flex">
					<span style="width:50%;line-height: 34px;">标签名称：</span><input class="form-control"
						name="itemname" style="width:50%">
				</div>
				<div class="col-md-4" style="display: flex;">
					<span style="width: 20%;line-height: 34px;">数据源：</span>
					<input class="form-control" name="select1" placeholder="多个分类请用英文逗号分开"
						style="width: 80%;">
				</div>
				<div class="col-md-4" style="display: flex;">
					<span style="width: 30%;line-height: 34px;">二级筛选：</span>
					<input class="form-control" name="categorylist" placeholder="多个分类请用英文逗号分开"
						style="width: 70%;">
				</div> 
					<button type="button" class="btn btn-danger btn-delete">删除</button>		
				</div>
			</div>
			<div class="row col-md-12" name="tablist" id="YiigooCopy">
				<div class="col-md-5" style="display:flex">
					<span style="width:15%;line-height: 34px;">显示文字：</span><input class="form-control"
						name="itemname" style="width:50%">
				</div>
				<div class="col-md-5" style="display: flex;">
					<span style="width: 20%;line-height: 34px;">查询分类项：</span>
					<input class="form-control" name="categorylist" placeholder="多个分类请用英文逗号分开"
						style="width: 50%;">
				</div>
				<div class="col-md-2" style="display: flex;">
					<button type="button" class="btn btn-danger btn-delete">删除</button>		
				</div>
			</div>
			<div class="modal fade" id="box-config" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title">一级标签</h4>
							</div>
							<div class="modal-body">
								<div class="form-group">
									<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>标签名称：</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="keywords" placeholder="请输入标签名称" />
										<input type="hidden" name="id" />

									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>数据源：</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="sortname" placeholder="请输入数据源" />
									</div>
								</div>
							</div>

							<div class="modal-footer">
								<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
								<button type="button" class="btn btn-success bt-save" nt='box-config'>保存</button>
							</div>
						</div>
					</div>
				</div>
				<div class="modal fade" id="box-two" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						<div class="modal-dialog">
							<div class="modal-content">
								<div class="modal-header">
									<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
									<h4 class="modal-title">二级标签</h4>
								</div>
								<div class="modal-body">
									<div class="form-group">
										<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>标签名称：</label>
										<div class="col-md-8">
											<input type="text" class="form-control" name="keywords" placeholder="请输入标签名称" />
											<input type="hidden" name="id" />
	
										</div>
									</div>
									<div class="form-group">
										<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>链接地址：</label>
										<div class="col-md-8">
											<input type="text" class="form-control" name="sortname" placeholder="请输入数据源" />
										</div>
									</div>
								</div>
	
								<div class="modal-footer">
									<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
									<button type="button" class="btn btn-success bt-save" nt='box-two'>保存</button>
								</div>
							</div>
						</div>
					</div>
	</section>
	<!--/PAGE -->
	<!-- JAVASCRIPTS -->
	<!-- Placed at the end of the document so the pages load faster -->
	<!-- JQUERY -->
	<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
	<!-- JQUERY UI-->
	<script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
	<!-- BOOTSTRAP -->
	<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>
	<!-- BLOCK UI -->
	<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
	<!-- AUTOSIZE -->
	<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
	<!-- INPUT MASK -->
	<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
	<!-- SELECT2 -->
	<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
	<!-- UNIFORM -->
	<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
	<script type="text/javascript" src="../../js/ztree/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="../../js/ztree/jquery.ztree.excheck.js"></script>

	<!--bootbox-->
	<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
	<script type="text/javascript" src="../../js/ztree/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="../../js/ztree/jquery.ztree.excheck.js"></script>
	<script type="text/javascript" src="../../js/ztree/jquery.ztree.exedit.js"></script>
	<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
	<!-- COOKIE -->
	<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
	<!-- CUSTOM SCRIPT -->
	<script src="../../js/script.js"></script>
	<script src="../../js/common.js"></script>
	<script>
		
		var Group = function () {
			var setting, zNodes, zTree, mode, copyYijiadian, zTreeNode;
			var isCreate = false;
			var initCategoryTree = function () {
				zNodes = [];
				$.get(App.getContextPath() + "admin/promotion/getHomeTab.do", {}, function (result) {
					if (result.extra) {
						if (result.extra.Yiigoo) {
							$.each(JSON.parse(result.extra.Yiigoo), function (index, item) {
								console.log(item)
								var copy = $('#YiigooCopy').clone().attr('id', '');
								copy.find('[name=itemname]').val(item.itemName);
								copy.find('[name=categorylist]').val(item.categoryList);
								$("[name=Yiigoo]").append(copy);
							})
						}
						if (result.extra.Yijiadian) {
							var Yijiadian = JSON.parse(result.extra.Yijiadian)
							copyYijiadian = [];
							$.each(Yijiadian, function (index, item) {
								copyYijiadian.push({
									id: index + 1,
									parentId: 0,
									itemName: item.itemName,
									level: 1,
									select1: item.select1
								})
								var categoryList = item.itemList || [];
								$.each(categoryList, function (x, t) {
									copyYijiadian.push({
										parentId: index + 1,
										itemName: t.itemName,
										linkUrl: t.linkUrl,
										level: 2
									})
								})
							})
							$.each(copyYijiadian, function (index, item) {
								zNodes.push({id: item.id, pId: item.parentId, name: item.itemName, select1: item.select1, open: (item.level === 1), linkUrl: item.linkUrl});
							})
							zTree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
							// $.each(JSON.parse(result.extra.Yijiadian), function (index, item) {
							// 	var copy = $('#YijiadianCopy').clone().attr('id', '');
							// 	copy.find('[name=itemname]').val(item.itemName);
							// 	copy.find('[name=categorylist]').val(item.categoryList);
							// 	copy.find('[name=select1]').val(item.select1);
							// 	$("[name=Yijiadian]").prepend(copy);
							// })
						}
					}
				});
			}
			var setHomeTab = function () {
				var tablist = [];
				var salesPlatform = $(this).attr("sales");
				$("div[name=" + salesPlatform + "]").find("div[name=tablist]").each(function () {
					var i = {}
					i.itemName = $(this).find("[name=itemname]").val();
					i.categoryList = $(this).find("[name=categorylist]").val()
					if (salesPlatform == "Yijiadian")
						i.select1 = $(this).find("[name=select1]").val()
					tablist.push(i)
				})

				if (tablist.length == 0) {
					$.alert("请检查输入项")
					return;
				}
				$.post(App.getContextPath() + "admin/promotion/setHomeTab.do", { tablist: JSON.stringify(tablist), salesPlatform: salesPlatform }, function (result) {
					if (result.type != 'success') {
						$.alert(result.extra);
					} else {
						$.alert("保存成功");
					}
				});
			}
			var addHoverDom = function(treeId, treeNode) {
			if (treeNode.level === 1) {
				return false;
			} else {
				//给节点加入"新增"button
				var sObj = $("#" + treeNode.tId + "_span");
				if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) {
					return;
				}
				var addStr = "<span class='button add' id='addBtn_" + treeNode.id
					+ "' title='新增分类' onfocus='this.blur();'></span>";
				sObj.after(addStr);
				var btn = $("#addBtn_"+treeNode.id);
				if (btn) {
					btn.bind("click", function(){
						zTreeNode = treeNode
						mode = 'add';
						$("input[name=keywords]").val('')
						$("input[name=sortname]").val('')
						$("#box-two").modal("show");
					});
				}
			}
		}
		var removeHoverDom = function(treeId, treeNode) {
			$("#addBtn_"+treeNode.id).unbind().remove();
		}
		var tempNodeName = "";
		var beforeRename  = function(treeId, treeNode, newName, isCancel) {
			console.log(treeNode)
			zTreeNode = treeNode
			if (isCreate == 'create') {
				zTreeNode.id = 'new'
				zTree.updateNode(zTreeNode);
				isCreate = false
				return true;
			}
			if (isCreate == 'add') {
				zTreeNode.id = ''
				zTree.updateNode(zTreeNode);
				isCreate = false
				return true;
			}
			if (treeNode.id) {
				mode = 'itemName';
				var itemName = treeNode.name;
				var select1 = treeNode.select1;
				$("input[name='keywords']").val(itemName)
				$("input[name='sortname']").val(select1)
				$("#box-config").modal("show");
			} else {
				mode = 'editItem'
				var itemName = treeNode.name;
				var linkUrl = treeNode.linkUrl;
				$("input[name='keywords']").val(itemName)
				$("input[name='sortname']").val(linkUrl)
				$("#box-two").modal("show");
			}
			return true;
		}
		var onRename = function(event, treeId, treeNode, isCancel) {
			renameCategory(treeNode);
		}
		var renameCategory = function(treeNode) {
			if (treeNode.name == '' || treeNode.name == tempNodeName) {
				treeNode.name = tempNodeName;
				zTree.updateNode(treeNode);
				return;
			}
		}
		var beforeRemove = function(treeId, treeNode) {
			$.confirm("确认删除【" + treeNode.name + "】分类吗？", function(){
				zTree.removeNode(treeNode);
			});
			return false;
		}
		var filterLinkUrl = function (linkUrl) {
			var img = {}
			if(linkUrl.indexOf("product.html")>0){
				img.imageType="Product";
				var i=linkUrl.indexOf("?");
				var k=linkUrl.substr(i+1,linkUrl.length).split("&");			
				img.searchKey=k[0].replace("productId=","");
			}else if(linkUrl.indexOf("static")>0){
				img.imageType="WebView";					
				img.searchKey="";
			}else if(linkUrl.indexOf("category.html")>0){
				img.imageType="Category";					
				img.searchKey="";
			}else if(linkUrl.indexOf("list.html")>0){
				img.imageType="List";
				var i=linkUrl.indexOf("?");
				var k=linkUrl.substr(i+1,linkUrl.length);			
				img.searchKey = k;
				// var k=linkUrl.substr(i+1,linkUrl.length).split("&");			
				// img.searchKey=k[0].replace("tags=","").replace("key=","");
			}else if(linkUrl.indexOf("seconds.html")>0){
				img.imageType="Seconds";					
				img.searchKey="";
			}else if(linkUrl.indexOf("newuser.html")>0){
				img.imageType="Newuser";					
				img.searchKey="";
			}else if(linkUrl.indexOf("bargain.html")>0){
				img.imageType="Bargain";					
				img.searchKey="";
			}else if(linkUrl.indexOf("agent/index.html")>0){
				img.imageType="AgentAdvantage";					
				img.searchKey="";
			}else if(linkUrl.indexOf("coupon.html")>0){
				img.imageType="Coupon";					
				var i=linkUrl.indexOf("?");
				var k=linkUrl.substr(i+1,linkUrl.length).split("&");			
				img.searchKey = k[0].replace("couponId=","");
			}
			return img
		}
			return {
				init: function () {
					$(".bt_updateTab").click(setHomeTab);
					$('body').on('click', '.btn-delete', function() {
						var $this = $(this);
						$.confirm("确认删除该分类数据吗？", function(){
							$this.closest('.row').remove()
						});
					});
					$(".create").click(function () {
						mode = 'create';
						$("input[name=keywords]").val('')
						$("input[name=sortname]").val('')
						$("#box-config").modal("show");
					});
					$('body').on('click', '.bt-save', function() {
						var nt = $(this).attr('nt')
						console.log(nt)
						var keywords = $("#" + nt + " input[name=keywords]").val()
						var sortname = $("#" + nt + " input[name=sortname]").val()
						if (!keywords) {
							$("#" + nt).find(".form-group").addClass("has-error");
							return;
						} else if(!sortname){
							$("#" + nt).find(".form-group").addClass("has-error");
							return;
						}else {
							$("#" + nt).modal("hide");
							if (mode == 'itemName') {
								zTreeNode.select1 = sortname;
								zTreeNode.name = keywords
								zTree.updateNode(zTreeNode);
							} else if (mode == 'create') {
								var node = zTree.addNodes(null, {id: '', pId: 0, name: keywords, open: true, select1: sortname});
								zTree.editName(node[0]);
								isCreate = 'create';
							} else if (mode == 'add') {
								var node = zTree.addNodes(zTreeNode, {id: '', pId: zTreeNode.id, name: keywords, open: true, linkUrl: sortname});
								zTree.editName(node[0]);
								isCreate = 'add';
							} else if (mode = 'editItem') {
								zTreeNode.linkUrl = sortname;
								zTreeNode.name = keywords
								zTree.updateNode(zTreeNode);
							}
						}
					})
					$("body").on('click', '.save-yijiadian', function () {
						var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
						var nodes=treeObj.getNodes(true);
						if (!nodes.length) {
							$.alert("至少添加一个一级分类");
						}
						var array = []
						nodes.forEach(function (element) {
							var item = {};
							item.itemName = element.name
							item.select1 = element.select1
							if(element.children) {
								item.itemList = []
								element.children.forEach(function (t) {
									var obj = {
										itemName: t.name,
										linkUrl: t.linkUrl
									}
									var img = filterLinkUrl(t.linkUrl);
									if (img.imageType) {
										obj.imageType = img.imageType
										obj.searchKey = img.searchKey
									}
									item.itemList.push(obj)
								})
							}
							array.push(item)
						});
						$.post(App.getContextPath() + "admin/promotion/setHomeTab.do", { tablist: JSON.stringify(array), salesPlatform: 'Yijiadian' }, function (result) {
							if (result.type != 'success') {
								$.alert(result.extra);
							} else {
								$.alert("保存成功");
							}
						});
					})
					setting = {
			    		view: {
			            	addHoverDom: addHoverDom,
			            	removeHoverDom: removeHoverDom,
			            	selectedMulti: false,
			    		},
			         	check: {
			            	enable: false
			            },
			            data: {
			                simpleData: {
			                    enable: true
			                }
			            },
			            edit: {
			                enable: true, 
			                drag: false,
			    			removeTitle: "删除分类",
			    			renameTitle: "编辑分类名称"
			            },
			            callback: {
							beforeRemove: beforeRemove,
							beforeRename: beforeRename,
			    			onRename: onRename
						}
			        }
					initCategoryTree();
				}
			}
		}();
		$(function () {
			$("#header").load("../head.html");
			App.init();
			Group.init();
		});
	</script>
	<!-- /JAVASCRIPTS -->
</body>

</html>